package com.dh.project.controller;

import com.dh.project.model.*;
import com.dh.project.other.ModelRes;
import com.dh.project.service.*;
import com.dh.project.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;


/**
 * Created by admin on 2017/4/13.
 */
@Controller
public class LogisticsController {

    private EngineerPhoneService engineerPhoneService;

    private ServiceProjectService serviceProjectService;

    private LableInfoService lableInfoService;

    private CouponService couponService;

    private CouponPeopleService couponPeopleService;

    @Autowired
    private LogisticsController(EngineerPhoneService engineerPhoneService,ServiceProjectService serviceProjectService,
                                LableInfoService lableInfoService,CouponService couponService,CouponPeopleService couponPeopleService){
        this.engineerPhoneService = engineerPhoneService;
        this.serviceProjectService = serviceProjectService;
        this.lableInfoService = lableInfoService;
        this.couponService = couponService;
        this.couponPeopleService = couponPeopleService;
    }


    /**
     * 工程师电话列表
     * @param model
     * @return
     */
    @RequestMapping(value = "selectEngineerPhoneList",produces = "text/html;charset=utf-8")
    public String selectEngineerPhoneList(EngineerPhone engineerPhone,int page,Model model){
        Map map = new HashMap();
        if (engineerPhone.getEngineer_id() != null){
            map.put("engineer_id",engineerPhone.getEngineer_id());
        }
        if (engineerPhone.getEngineer_name() != null){
            map.put("engineer_name",engineerPhone.getEngineer_name());
        }
        map.put("page_start",(page-1)*10);
        map.put("page_end",10);
        List<EngineerPhone> engineerPhoneList = engineerPhoneService.selectEngineerPhoneListByMap(map);

        PageModel pageModel = engineerPhoneService.selectEngineerPhoneListByMapCount(map);
        PageInfo pageInfo = PageUtil.getPageInfo(pageModel,page,10);
        pageInfo.setPage_href(PageUtil.getPageHref(engineerPhone,4));
        pageInfo.setPage_index(page);
        pageInfo.setPage_count(PageUtil.getPageCount(pageModel.getPage_index(),10));

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("engineerPhone",engineerPhone);
        model.addAttribute("engineerPhoneList",engineerPhoneList);
        return "engineer_list";
    }

    /**
     * 添加工程师电话
     * @param engineerPhone
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "insertEngineerPhone",produces = "text/html;charset=utf-8")
    public Object insertEngineerPhone(EngineerPhone engineerPhone){
        Map map = new HashMap();
        map.put("engineer_phone",engineerPhone.getEngineer_phone());
        List<EngineerPhone> engineerPhones = engineerPhoneService.selectByMap(map);
        if (engineerPhones.size()>0){
            return new ModelRes(ModelRes.Status.ERROR,"该手机号码已存在!");
        }

        boolean isSuccess = engineerPhoneService.insert(engineerPhone);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"添加失败!");
        }
    }

    /**
     * 修改工程师
     * @param engineerPhone
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateEngineerPhone",produces = "text/html;charset=utf-8")
    public Object updateEngineerPhone(EngineerPhone engineerPhone){
        Map map = new HashMap();
        map.put("engineer_phone",engineerPhone.getEngineer_phone());
        List<EngineerPhone> engineerPhones = engineerPhoneService.selectByMap(map);
        if (engineerPhones.size() != 0){
            if (engineerPhones.get(0).getEngineer_id() != engineerPhone.getEngineer_id()){
                return new ModelRes(ModelRes.Status.ERROR,"该手机号码已存在，修改失败!");
            }
        }

        EngineerPhone engineerPhone1 = engineerPhoneService.selectById(engineerPhone.getEngineer_id());
        if (engineerPhone.getEngineer_name() != null){
            engineerPhone1.setEngineer_name(engineerPhone.getEngineer_name());
        }
        if (engineerPhone.getEngineer_phone() != null){
            engineerPhone1.setEngineer_phone(engineerPhone.getEngineer_phone());
        }
        if (engineerPhone.getPhone_address() != null){
            engineerPhone1.setPhone_address(engineerPhone.getPhone_address());
        }
        boolean isSuccess = engineerPhoneService.updateById(engineerPhone1);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"修改失败!");
        }
    }

    /**
     * 删除工程师
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteEngineer",produces = "text/html;charset=utf-8")
    public Object deleteEngineer(int admin_id){
        boolean isSuccess = engineerPhoneService.deleteById(admin_id);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"删除失败!");
        }
    }

    /**
     * 查询标签
     * @param lableInfo
     * @param page
     * @param model
     * @return
     */
    @RequestMapping(value = "selectLableInfo",produces = "text/html;charset=utf-8")
    public String selectLableInfo(LableInfo lableInfo, int page, Model model){
        Map map = new HashMap();
        if (lableInfo.getLable_type() != null){
            map.put("lable_type",lableInfo.getLable_type());
        }else {
            map.put("lable_type",0);
        }
        if (lableInfo.getLable_class_one() != null){
            map.put("lable_class_one",lableInfo.getLable_class_one());
        }else {
            map.put("lable_class_one",0);
        }
        map.put("page_start",(page - 1)*10);
        map.put("page_end",10);
        List<LableInfo> lableInfos = lableInfoService.selectLableInfoByMap(map);

        PageModel pageModel = lableInfoService.selectLableInfoByMapCount(map);
        PageInfo pageInfo = PageUtil.getPageInfo(pageModel,page,10);
        pageInfo.setPage_href(PageUtil.getPageHref(lableInfo,5));
        pageInfo.setPage_index(page);
        pageInfo.setPage_count(PageUtil.getPageCount(pageModel.getPage_index(),10));
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("lableInfo",lableInfo);
        model.addAttribute("lableInfos",lableInfos);
        return "lable_list";
    }

    /**
     * 添加标签
     * @param lableInfo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "insertLableInfo",produces = "text/html;charset=utf-8")
    public Object insertLableInfo(LableInfo lableInfo){
        lableInfo.setModifytime(String.valueOf(new Date().getTime()));
        lableInfo.setCreatetime(String.valueOf(new Date().getTime()));
        boolean isSuccess = lableInfoService.insert(lableInfo);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"添加失败！");
        }
    }

    /**
     * 修改标签
     * @param lableInfo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateLableInfo",produces = "text/html;charset=utf-8")
    public Object updateLableInfo(LableInfo lableInfo){
        LableInfo lableInfo1 = lableInfoService.selectById(lableInfo.getLable_id());
        lableInfo1.setLable_name(lableInfo.getLable_name());
        lableInfo1.setLable_class_one(lableInfo.getLable_class_one());
        lableInfo1.setLable_class_two(lableInfo.getLable_class_two());
        lableInfo1.setLable_type(lableInfo.getLable_type());
        lableInfo1.setLable_name(lableInfo.getLable_name());
        lableInfo1.setModifytime(String.valueOf(new Date().getTime()));

        Map map = new HashMap();
        if (lableInfo.getLable_type() == 1){
            map.put("service_brand",lableInfo.getLable_id());
        }
        if (lableInfo.getLable_type() == 2){
            map.put("service_assembly",lableInfo.getLable_id());
        }
        if (lableInfo.getLable_type() == 3){
            map.put("service_standard",lableInfo.getLable_id());
        }
        List<ServiceProject> serviceProjects = serviceProjectService.selectByMap(map);
        for (int i = 0; i < serviceProjects.size(); i++) {
            if (lableInfo.getLable_type() == 1){
                serviceProjects.get(i).setService_brand_name(lableInfo.getLable_name());
            }else if (lableInfo.getLable_type() == 2){
                serviceProjects.get(i).setService_assembly_name(lableInfo.getLable_name());
            }else if (lableInfo.getLable_type() == 3){
                serviceProjects.get(i).setService_standard_name(lableInfo.getLable_name());
            }
            serviceProjectService.updateById(serviceProjects.get(i));
        }
        boolean isSuccess = lableInfoService.updateById(lableInfo1);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"修改失败！");
        }
    }

    /**
     *  删除标签
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteLable",produces = "text/html;charset=utf-8")
    public Object deleteLable(int admin_id){
        boolean isSuccess = lableInfoService.deleteById(admin_id);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"删除失败！");
        }
    }

    /**
     * 查询优惠券活动列表
     * @param coupon
     * @param page
     * @param model
     * @return
     */
    @RequestMapping(value = "selectCouponList",produces = "text/html;charset=utf-8")
    public String selectCouponList(Coupon coupon, int page, Model model){
        Map map = new HashMap();
        if (coupon.getCoupon_name() != null){
            map.put("coupon_name",coupon.getCoupon_name());
        }
        if (coupon.getId() != null){
            map.put("id",coupon.getId());
        }
        map.put("page_start",(page - 1)*10);
        map.put("page_end",10);

        List<Coupon> coupons = couponService.selectCouponListByMap(map);

        PageModel pageModel = couponService.selectCouponListByMapCount(map);
        PageInfo pageInfo = PageUtil.getPageInfo(pageModel,page,10);
        pageInfo.setPage_href(PageUtil.getPageHref(coupon,6));
        pageInfo.setPage_index(page);
        pageInfo.setPage_count(PageUtil.getPageCount(pageModel.getPage_index(),10));

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("coupon",coupon);
        model.addAttribute("coupons",coupons);
        return "coupon_list";
    }


    /**
     * 查询优惠券活动详情
     * @param coupon
     * @param page
     * @param model
     * @return
     */
    @RequestMapping(value = "selectCouponDetail",produces = "text/html;charset=utf-8")
    public String selectCouponDetail(Coupon coupon, int page, Model model){
        Coupon coupon1 = couponService.selectById(coupon.getId());
        Map map  = new HashMap();
        map.put("coupon_id",coupon.getId());
        map.put("id",coupon.getId());
        map.put("page_start",(page - 1)*5);
        map.put("page_end",5);

        List<CouponePeople> couponePeoples = couponPeopleService.selectCouponpeopleByMap(map);
        PageModel pageModel = couponPeopleService.selectCouponpeopleByMapCount(map);
        PageInfo pageInfo = PageUtil.getPageInfo(pageModel,page,5);
        pageInfo.setPage_href(PageUtil.getPageHref(coupon,7));
        pageInfo.setPage_index(page);
        pageInfo.setPage_count(PageUtil.getPageCount(pageModel.getPage_index(),5));

        model.addAttribute("pageInfo",pageInfo);
        System.out.println(coupon1.toString());
        model.addAttribute("coupon",coupon1);
        model.addAttribute("couponePeoples",couponePeoples);
        return "coupon_detail";
    }

    /**
     * 查询优惠券领取记录列表
     * @param coupon
     * @param page
     * @param model
     * @return
     */
    @RequestMapping(value = "selectCouponGetList",produces = "text/html;charset=utf-8")
    public String selectCouponGetList(Coupon coupon, int page, Model model){

        Map map1  = new HashMap();
        if (coupon.getId() != null){
            map1.put("coupon_id",coupon.getId());
        }
        if (coupon.getCoupon_name() != null){
            map1.put("coupon_name",coupon.getCoupon_name());
        }

        map1.put("page_start",(page - 1)*10);
        map1.put("page_end",10);

        List<CouponePeople> couponePeopleList = couponPeopleService.selectGetCouponpeopleByMap(map1);


        List<Coupon> coupons = couponService.selectByMap(new HashMap());
        model.addAttribute("coupons",coupons);

        PageModel pageModel = couponPeopleService.selectGetCouponpeopleByMapCount(map1);
        PageInfo pageInfo = PageUtil.getPageInfo(pageModel,page,10);
        pageInfo.setPage_href(PageUtil.getPageHref(coupon,8));
        pageInfo.setPage_index(page);
        pageInfo.setPage_count(PageUtil.getPageCount(pageModel.getPage_index(),10));

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("coupon",coupon);
        model.addAttribute("couponePeopleList",couponePeopleList);
        return "coupon_get_list";
    }

    /**
     * 修改优惠券活动信息
     * @param coupon
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateCoupon",produces = "text/html;charset=utf-8")
    public Object updateCoupon(Coupon coupon){
        Coupon coupon1 = couponService.selectById(coupon.getId());
        coupon1.setCoupon_name(coupon.getCoupon_name());
        coupon1.setCoupon_status(coupon.getCoupon_status());
        int count = coupon.getCount_number() - coupon1.getCount_number();
        if ((count + coupon1.getOut_count()) < 0){
            return new ModelRes(ModelRes.Status.ERROR,"减少总数不能高于剩余总量！");
        }else {
            coupon1.setCount_number(coupon.getCount_number());
            coupon1.setOut_count(coupon1.getOut_count() + count);
        }
        coupon1.setModifytime(String.valueOf(new Date().getTime()));
        boolean isSuccess = couponService.updateById(coupon1);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"修改失败！");
        }
    }

    /**
     * 删除优惠券活动
     * @param admin_id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteCoupon",produces = "text/html;charset=utf-8")
    public Object deleteCoupons(int admin_id){
        Coupon coupon1 = couponService.selectById(admin_id);
        if(coupon1.getCoupon_status() == 1){
            return new ModelRes(ModelRes.Status.ERROR,"该活动正在进行中不能删除！");
        }else {
            boolean isSuccess = couponService.deleteById(admin_id);
            if (isSuccess){
                return new ModelRes();
            }else {
                return new ModelRes(ModelRes.Status.ERROR,"删除失败！");
            }
        }
    }

    /**
     * 添加优惠券活动
     * @param coupon
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "insertCoupon",produces = "text/html;charset=utf-8")
    public Object insertCoupon(Coupon coupon){
        Map map = new HashMap();
        map.put("coupon_status",1);
        List<Coupon> coupons = couponService.selectByMap(map);
        if (coupons.size()>0){
            return new ModelRes(ModelRes.Status.ERROR,"已有优惠券活动正在进行中不能添加！");
        }

        if(Long.valueOf(coupon.getUseful_time()) < new Date().getTime()){
            return new ModelRes(ModelRes.Status.ERROR,"该有效期已过期！");
        }

        coupon.setModifytime(String.valueOf(new Date().getTime()));
        coupon.setOut_count(coupon.getCount_number());
        coupon.setCoupon_status(1);
        boolean isSuccess = couponService.insert(coupon);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"添加失败");
        }
    }

    /**
     * 暂停或开始优惠券活动
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "startOrStopCoupon",produces = "text/html;charset=utf-8")
    public Object startOrStopCoupon(int id){
        Map map = new HashMap();
        map.put("coupon_status",1);
        List<Coupon> coupons = couponService.selectByMap(map);
        if (coupons.size() > 0){
            if (coupons.get(0).getId() != id){
                return new ModelRes(ModelRes.Status.ERROR,"已有优惠券活动正在进行中不能开始其他优惠券活动！");
            }
        }
        Coupon coupon = couponService.selectById(id);
        if (coupon.getCoupon_status() == 1){
            coupon.setCoupon_status(2);
        }else if (coupon.getCoupon_status() == 2){
            if (Long.valueOf(coupon.getUseful_time()) < new Date().getTime()){
                return new ModelRes(ModelRes.Status.ERROR,"该优惠券活动已过期，不能开始!");
            }
            coupon.setCoupon_status(1);
        }
        coupon.setModifytime(String.valueOf(new Date().getTime()));
        boolean isSuccess = couponService.updateById(coupon);
        if (isSuccess){
            return new ModelRes();
        }else {
            return new ModelRes(ModelRes.Status.ERROR,"操作失败");
        }
    }

}
